import axios from 'axios'
import qs from 'qs'

function myAxios (option) {
  window.LoadingBar.start()
  // axios post 提交数据，后台获取不到提交的数据解决方案
  if (option.headers === undefined && option.method === 'POST' && option.data) {
    axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
    // 坑，qs.stringify处理的对象中，key对应的值不能有json字符串（对象or数组）
    option.data = qs.stringify(option.data)
  }
  axios.defaults.baseURL = '/apis'
  return axios(option).then(function (response) {
      window.LoadingBar.finish()
      let st = response.data.status
      if (st === 555) {
        window.vm.$router.push({ path: '/login' })
        let msg = response.data.msg ? response.data.msg : '请重新登陆！'
        window.vm.$Message.error(msg)
        return
      }
      return Promise.resolve(response)
    }).catch(function (error) {
      window.LoadingBar.error()
      let st = error.response.data.status
      if (st === 555 || st === 403) {
        window.vm.$router.push({ path: '/login' })
        let msg = error.response.data.msg ? error.response.data.msg : '请重新登陆！'
        window.vm.$Message.error(msg)
        return
      }
      return Promise.reject(error)
    })
}
// `responseType` 表示服务器响应的数据类型，可以是 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'
// responseType: 'json', // 默认的
// get传参数用params,
// 'PUT', 'POST', 和 'PATCH'传参数用 data
let api = {
  do_get: function (url) {
    // 通用 get 接口
    let option = {method: 'GET', url: url}
    return myAxios(option)
  },
  do_post: function (url, data) {
    // 通用 post 接口
    let option = {method: 'POST', url: url, data: data,}
    return myAxios(option)
  },
  kaptcha: function () {
    // 接受二进制流数据
    let option = { method: 'GET', url: '/kaptcha.jpg', responseType: 'blob' }
    return myAxios(option)
  },
  show_pic: function (data) {
    // 接受二进制流数据
    let option = { method: 'POST', url: '/$pic/visit', data: data, responseType: 'arraybuffer' }
    return myAxios(option)
  },
  do_del: function (url, data) {
    // 通用 删除 调用
    let option = {
      method: 'DELETE',
      url: url,
      data: JSON.stringify(data),
      headers: {'Content-Type': 'application/json;charset=UTF-8'}
    }
    return myAxios(option)
  },
  login: function (data) {
    let option = { method: 'POST', url: '/login', data: data }
    return myAxios(option)
  },
  upload: function (url, data) {
    let option = { method: 'POST', url: url, data: data, headers: { 'Content-Type': 'multipart/form-data' } }
    return myAxios(option)
  },
  // 后台配置---数据
  quota_save: function (data) {
    let option = { method: 'POST', url: '/$quota/save', data: data, headers: { 'Content-Type': 'application/json;charset=UTF-8' } }
    return myAxios(option)
  },
  quota_del: function (data) {
    let option = { method: 'DELETE', url: '/$quota/del', data: JSON.stringify(data), headers: { 'Content-Type': 'application/json;charset=UTF-8' } }
    return myAxios(option)
  },
  quota_info_id: function (data) {
    let option = { method: 'GET', url: '/$quota/info/' + data.id }
    return myAxios(option)
  },
  quota_list: function (data) {
    let option = { method: 'GET', url: '/$quota/list', params: data }
    return myAxios(option)
  },
  quota_upd: function (data) {
    let option = { method: 'PUT', url: '/$quota/upd', data: data }
    return myAxios(option)
  },
  // 安全监控---视频
  security_dev_sums: function (data) {
    let option = { method: 'POST', url: '/$security/dev&sums', data: data, headers: { 'Content-Type': 'application/json;charset=UTF-8' } }
    return myAxios(option)
  },
  // 能源监控---碳排&对比
  // 汇总碳排,单车碳排,综合产量
  carbon_total: function (data) {
    let option = { method: 'POST', url: '/$carbon/total', data: data }
    return myAxios(option)
  },
  // 单类能源碳排
  carbon_energy: function (data) {
    let option = { method: 'POST', url: '/$carbon/energy', data: data }
    return myAxios(option)
  },
  // 汇总--碳排走势
  carbon_total_chart: function (data) {
    let option = { method: 'POST', url: '/$carbon/total&chart', data: data }
    return myAxios(option)
  },
  // 汇总--子级对比
  carbon_total_sublevel_chart: function (data) {
    let option = { method: 'POST', url: '/$carbon/total&sublevel&chart', data: data }
    return myAxios(option)
  },
  // 单车--碳排走势
  carbon_avg_chart: function (data) {
    let option = { method: 'POST', url: '/$carbon/trendAvg', data: data }
    return myAxios(option)
  },
  // 单车--子级对比
  carbon_avg_sublevel_chart: function (data) {
    let option = { method: 'POST', url: '/$carbon/avg&sublevel&chart', data: data }
    return myAxios(option)
  },
  // --子级对比
  carbonSubset: function (data) {
    let option = { method: 'POST', url: '/$carbon/subset', data: data }
    return myAxios(option)
  },
  // 能源监控--对比
  carbon_comparison: function (data) {
    let option = { method: 'POST', url: '/$carbon/comparison', data: data }
    return myAxios(option)
  },
  // 设备类型接口
  devType_save: function (data) {
    let option = { method: 'POST', url: '/iot/devType/save', data: data, headers: { 'Content-Type': 'application/json;charset=UTF-8' } }
    return myAxios(option)
  },
  devType_del: function (data) {
    let option = { method: 'DELETE', url: '/iot/devType/del', data: JSON.stringify(data), headers: { 'Content-Type': 'application/json;charset=UTF-8' } }
    return myAxios(option)
  },
  devType_list: function (data) {
    let option = { method: 'GET', url: '/iot/devType/list', params: data }
    return myAxios(option)
  },
  devType_upd: function (data) {
    let option = { method: 'PUT', url: '/iot/devType/upd', data: data }
    return myAxios(option)
  },
  dev_icons: function (data) {
    let option = {method: 'GET', url: '/iot/devType/icons', data: data}
    return myAxios(option)
  },
  // 设备接口
  dev_save: function (data) {
    let option = { method: 'POST', url: '/iot/dev/save', data: data, headers: { 'Content-Type': 'application/json;charset=UTF-8' } }
    return myAxios(option)
  },
  dev_del: function (data) {
    let option = { method: 'DELETE', url: '/iot/dev/del', data: JSON.stringify(data), headers: { 'Content-Type': 'application/json;charset=UTF-8' } }
    return myAxios(option)
  },
  dev_list: function (data) {
    let option = { method: 'GET', url: '/iot/dev/list', params: data }
    return myAxios(option)
  },
  dev_upd: function (data) {
    let option = { method: 'PUT', url: '/iot/dev/upd', data: data }
    return myAxios(option)
  },
  dev_sid: function (data) {
    let option = { method: 'POST', url: '/iot/dev/sid', data: data, headers: { 'Content-Type': 'application/json;charset=UTF-8' } }
    return myAxios(option)
  },
  // 空间接口
  space_save: function (data) {
    let option = { method: 'POST', url: '/iot/space/save', data: data, headers: { 'Content-Type': 'application/json;charset=UTF-8' } }
    return myAxios(option)
  },
  space_del: function (data) {
    let option = { method: 'DELETE', url: '/iot/space/del', data: JSON.stringify(data), headers: { 'Content-Type': 'application/json;charset=UTF-8' } }
    return myAxios(option)
  },
  space_list: function (data) {
    let option = { method: 'GET', url: '/iot/space/list', params: data }
    return myAxios(option)
  },
  space_upd: function (data) {
    let option = { method: 'PUT', url: '/iot/space/upd', data: data }
    return myAxios(option)
  },
  space_info: function (data) {
    let option = { method: 'GET', url: '/iot/space/info', params: data }
    return myAxios(option)
  },
  space_tree: function (data) {
    //  获取 空间的 级联数选择数据
    let option = { method: 'GET', url: '/iot/space/cas', params: data }
    return myAxios(option)
  },
  // 首页关注设备数据
  follow_devs: function (data) {
    //  获取 当前用户关注的设备信息
    let option = { method: 'POST', url: '/$follow/list', data: data }
    return myAxios(option)
  },
  do_follow: function (data) {
    //  关注设备
    let option = {
      method: 'POST',
      url: '/$follow/follow',
      data: JSON.stringify(data),
      headers: { 'Content-Type': 'application/json;charset=UTF-8' }
    }
    return myAxios(option)
  },
  cancel_follow: function (data) {
    //  取消关注设备
    let option = {
      method: 'POST',
      url: '/$follow/cancel',
      data: JSON.stringify(data),
      headers: { 'Content-Type': 'application/json;charset=UTF-8' }
    }
    return myAxios(option)
  },
  cur_data: function (data) {
    //  获取实时数据
    let option = { method: 'POST', url: '/iot/dev/curData', data: data }
    return myAxios(option)
  },
  // 字典数据
  dict_list: function (data) {
    let option = {method: 'GET', url: '/dict/list', params: data}
    return myAxios(option)
  },
  dict_save: function (data) {
    let option = {method: 'POST', url: '/dict/save', data: JSON.stringify(data), headers: {'Content-Type': 'application/json;charset=UTF-8'}}
    return myAxios(option)
  },
  dict_update: function (data) {
    let option = {method: 'PUT', url: '/dict/upd', data: JSON.stringify(data), headers: {'Content-Type': 'application/json;charset=UTF-8'}}
    return myAxios(option)
  },
  dict_delete: function (data) {
    let option = {method: 'DELETE', url: '/dict/del', data: JSON.stringify(data), headers: {'Content-Type': 'application/json;charset=UTF-8'}}
    return myAxios(option)
  },
  dict_opts: function (data) {
    let option = {method: 'GET', url: '/dict/opts', data: data}
    return myAxios(option)
  },
  sys_list: function (data) {
    let option = {method: 'POST', url: '/sysimg/list', data: data}
    return myAxios(option)
  },
  space_under_devs: function (data) {
    // 空间下 所有的子级关联的设备数据
    let option = {method: 'POST', url: 'iot/dev/spaceAllDevs', data: data}
    return myAxios(option)
  },
  // 设备数据查询
  iotDevRealData: function (data) {
    let option = {method: 'POST', url: '/iot/dev/realData', data: data}
    return myAxios(option)
  }
}
export default api
